package com.zy.design.demo.creational;

/**
 * 单例模式
 */
public class Singleton {
    private static Singleton instance;

    private Singleton() {
    }

    private String name;

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Singleton{" +
                "name='" + name + '\'' +
                '}';
    }

    public String printSuperString() {
        return super.toString();
    }
}

class Main4 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        instance.setName("张三");
        System.out.println(instance);
        System.out.println(instance.printSuperString());

        System.out.println("===============换个叫法===============");
        instance.setName("狗蛋");
        System.out.println(instance);
        System.out.println(instance.printSuperString());
    }
}
